package dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import moodle.dados.Curso;


public class MoodleDAO<T> {
	
	protected static EntityManagerFactory entityFactory;  
    protected static EntityManager entityManager;    
    
    static {  
        entityFactory = Persistence.createEntityManagerFactory("moodlePU");  
        entityManager = entityFactory.createEntityManager();        
        
    }
    
    public T find(Class<T> entityClass, Long id){
    	T result = (T) entityManager.find(entityClass, id);
    	return result;
    }
    
    public T find(Class<T> entityClass, int id){
    	T result = (T) entityManager.find(entityClass, id);
    	return result;
    }
    
    public List<T> find(String query){
    	return entityManager.createQuery(query).getResultList();
    }
    
    public T getReference(Class<T> entityClass, Long id){
    	return (T) entityManager.getReference(entityClass, id);
    }
    
    public static <K> K findNativeQuery(String query){
    	return (K) entityManager.createNativeQuery(query).getSingleResult();
    }
    
    public static <K> List<K> findNativeQueryList(String query){
    	return (List<K>) entityManager.createNativeQuery(query).getResultList();
    }
    
    public static Query findQueryParameterized(String query){
    	return entityManager.createQuery(query);
    }
    
    public static Query findNativeQueryParameterized(String query){
    	return entityManager.createNativeQuery(query);
    }
    
    public static Query createNamedQuery(String namedQuery){
    	return entityManager.createNamedQuery(namedQuery);
    }
    
    public static void closeEntityFactory(){
    	entityManager.close();
    	entityFactory.close();
    }
}
